<?php
ini_set('display_errors', 'Off');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Credentials:true');
$kuname = "longzs";
$qian="";
$qian2="";
$temparr=array();
require_once($_SERVER["DOCUMENT_ROOT"].'/myzhuaqu.php');
//require '../../vendor/autoload.php';
require '../phpQuery.php';
require '../Lib/ORG/Puhuo.php';
include("nodecode_inc.php");
include("jsondecode_inc.php");
$_REQUEST['url']=($_REQUEST['url']=="")?"":$_REQUEST['url'];
$_REQUEST['element']=($_REQUEST['element']=="")?"":$_REQUEST['element'];
$_REQUEST['element']=($_REQUEST['element']=="null")?null:$_REQUEST['element'];
$_REQUEST['data']=(trim($_REQUEST['data'])=="")?"":$_REQUEST['data'];

//获得所有注册过的抓取网站规则名称
$urlnames=getUrlNames();

if($_REQUEST['url']) {
    $host = host($_REQUEST['url']);
}elseif($_REQUEST['httpheader']){
    preg_match("/^(post|get)\s+([^\r\n]*)\s+http[^\r\n]*?\r??\n??(.*)(?:\r\n|\n\r|\n){2,}(.*)$/Uis",$_REQUEST['httpheader'],$nn);
    $_REQUEST['url']=trim($nn[2]);
    $host = host($nn[2]);
}
$result=array("host"=>$host);
$result['tojsdata']=getJsonReByHost($host);


$arr=getJsonHost($_REQUEST['url']);
if($_REQUEST['getArr']){
    echo json_encode($arr);
    exit;
}

$filters=$arr['filters'];

if(trim($_REQUEST["prodata"])!=""){
    savefilter($arr["id"]);
    $filters=$arr['filters']=$_REQUEST["filters"];
}

if($_REQUEST['saveFilters']){
    json_exit($_REQUEST['url']);
    exit;
}
//抓取
if($_REQUEST['geturl']!="") {
    $result['likere']=$arr['likere'];
    $result['name']=$arr['name'];
    if($_REQUEST['reset']&&file_exists("10000.html")){
        unlink("10000.html");
    }
//    if(!$isfinded) {
//        $result['msg']="没有匹配的网址!";
//        echo json_encode($result);
//        exit;
//    }

    list($result['data'],$result['data2'],$result['data3'],$result['data4'],$data5,$resultData)=getDataFromHttpHeader();
    $result['json']=json_encode($data0,JSON_UNESCAPED_UNICODE);
    $result['urlsnames']=getUrlNames($result);
    $result['data7']=getPhpCode();
    $result['filters']=$arr['filters'];
    $result['finally_prefix']=$arr['finally_prefix'];
    json2Table($resultData,"ls_goods","snowy","select id from snowy.ls_goods where goods_id='{\$data['goods_id']}'","id");
    echo json_encode($result);
    exit;
}


//双击filters框
if($_REQUEST['dblclick']!="") {
    $jsonstr=$_REQUEST['json'];
    if(preg_match("/\s*[a-zA-Z0-9_-]+\s*\((.*)\)\s*$/Uisx",$jsonstr,$nn)){
        $jsonstr=$nn[1];
    }
    $data0=json_decode($jsonstr, true);
    if(!is_array($data0)){
        $data0=nodecode($jsonstr);
    }
    list($data,$data2,$data3,$data4)=fx();
    $data4=print_r($data4,1);
    echo json_encode(array("data"=>$data,"data2"=>$data2,"data3"=>$data3,"data4"=>$data4));
    exit;
}

if($_REQUEST['getPhpCode']!="") {
    $result=array();
    list($result['data'],$result['data2'],$result['data3'],$result['data4'],$data0)=getDataFromHttpHeader();
    $result=empty($result['data4'])?$data0:$result['data4'];
    echo json_encode($result);
    exit;
}



if($_REQUEST['savecookie']!="") {
    $host = host($_REQUEST['url']);
    $cookie = urlencode($_REQUEST['cookie']);
    $result = array();
    execute("update json_host set cookie='{$_REQUEST['cookie']}' where host='{$host}'");
    postcookie($host,$_REQUEST['cookie']);
    $result = getrow("select cookie from json_host where host='{$host}'");
    echo json_encode($result);
    exit;
}

if($_REQUEST['getcookie']!="") {
    $host = host($_REQUEST['url']);
    $result = array();
    $result = getrow("select cookie from json_host where host='{$host}'");
    echo json_encode($result);
    exit;
}


if($_REQUEST['selecturl']!=""){
    $host=host($_REQUEST['url']);
    $url=$_REQUEST['url'];
    $result=selecturl($host,$url);
    echo json_encode($result);
    exit;
}

if($_REQUEST['save']!=""){
    list($id,$data)=save();
    if($id>0) {
        echo json_encode(array("code" => 1, "id" => $id, "mes" => "保存成功!","data"=>$data));
    }else{
        echo json_encode(array("code" =>0, "id" => $id, "mes" => "保存失败!","data"=>$data));
    }
    exit;
}



?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JSON查看</title>
    <link rel="stylesheet" href="/collect/reset/css/json.css">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" language="javascript" src="../js/layer/layer.js"></script>
    <script id="jsonscript" type="text/javascript" language="javascript" src="../js/json.js?v=0.1" ></script>
</head>
<form method="post">
    <input type="hidden" id="id" name="id" value="">
    <input type="hidden" id="host" name="host" value="">
    <input type="tempcookie" id="tempcookie" name="tempcookie" value="">
    <div class="formrow"><label>网址:</label><input type="text" id="url" name="url" value="<?=$_REQUEST['url']?>"/></div>
    <div class="formrow"><label>Likere:</label><input type="text" id="likere" name="likere" value="<?=$_REQUEST['likere']?>"/></div>
    <div class="formrow" style="height：26px;padding-top:9px">
        <div class="fl">
            <label>Method:</label>
            <input type="text" id="method" name="method" value="<?=$_REQUEST['method']?>"/>
        </div>
        <div class="fl">
            <label>Element:</label>
            <input type="text" id="element" name="element" value="<?=$_REQUEST['element']?>"/>
        </div>
        <div class="fl">
            <label>name:</label>
            <input type="text" id="name" name="name" value="<?=$_REQUEST['name']?>"/>
        </div>
        <div class="fl">
            <label>跳转:</label>
            <select type="text" id="selecturls" name="names" >
                <?php echo $urlnames;?>
            </select>
        </div>
    </div>
    <div class="formrow"><label>Header：</label><textarea id="header" name="header" style="height:50px"><?=$_REQUEST['header']?></textarea></div>
    <div class="formrow"><label>Cookie：</label><textarea id="cookie" name="cookie"><?=$_REQUEST['cookie']?></textarea></div>
    <div class="formrow">
        <label>Postdata：</label><textarea name="data" id="postdata"><?=$_REQUEST['data']?></textarea>
    </div>
    <div class="formrow">
        <div class="fl">
            <em>清缓存:</em><input type="checkbox" id="reset" name="reset" value="true"/>
        </div>
        <div class="fl">
            <em>GBK:</em><input type="checkbox" id="gbk" name="gbk" value="1"/>
        </div>
        <div class="fl">
            <a href="javascript:void" onclick="help('filter')">过滤帮助</a>
        </div>
    </div>
    <div class="formrow"><label>执行：</label>
        <input type="button" name="geturl" id="geturl" value="刷新" />&nbsp;&nbsp;
        <input type="button" name="save" id="save" value="保存" />&nbsp;&nbsp;
        <input type="button" name="getcookie" id="getcookie" value="取Cookie" />&nbsp;&nbsp;
        <input type="button" name="savecookie" id="savecookie" value="存Cookie" />&nbsp;&nbsp;
        <input type="button" name="testloginin" id="testloginin" value="重新登录" />&nbsp;&nbsp;
    </div>
    <div class="formrow"><label>获取数组前缀</label>
        <textarea name="finally_prefix" id="finally_prefix" style="height:30px"></textarea>
    </div>
    <div class="formrow"><label>页面json正则</label>
        <textarea name="tojsdata" id="tojsdata" style="height:30px"><?php echo $result['tojsdata'];?></textarea>
    </div>
    <div class="formrow"><label>结果：</label><em>(在下框双点鼠标左键解释该json)</em><textarea name="json" id="json" placehold="双点可解释该json"><?=print_r($html,1)?></textarea></div>
</form>

<div class="resone">
    <p></p>
    <div class="res">
        <textarea id="data" placeholder="data:Api数据"><?=htmlspecialchars(html_entity_decode($data))?></textarea>
        <textarea id="data2" placeholder="data2:Api数据结构"><?=htmlspecialchars(html_entity_decode($data2))?></textarea>
    </div>
</div>
<div class="filter">
    <p></p>
    <div class="res">
        <textarea id="filters" name="filters" placeholder="filters:过滤规则"><?=$_REQUEST['filters']?></textarea>
        <textarea id="data4" name="data4" placeholder="data4:最后过滤结果"></textarea>
    </div>
    <div class="res">
        <textarea id="data7" name="data7" placeholder="data7:php代码"></textarea>
        <input type="button" id="guolv" name="guolv" value="执行过滤">
    </div>
</div>

</body>
</html>

